package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tongatong
 * @name SupplierServiceImpl
 * @description
 * @date 2021/10/14
 * @time 17:12
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierDao supplierDao;

    @Override
    public PageInfo<Supplier> getSupplierList(Integer page, Integer rows, String supplierName) {
        //设置分页信息，该设置需在读取mapper中的方法前执行
        PageHelper.startPage(page, rows);
        List<Supplier> supplierList = supplierDao.selectSupplierList(supplierName);
        PageInfo<Supplier> pageInfo = new PageInfo<>(supplierList);
        return pageInfo;
    }

    @Override
    public void save(Supplier supplier, Integer supplierId) {
        if (null == supplierId) {
            supplierDao.insertSupplierById(supplier);
        }else {
            supplierDao.updateSupplierById(supplier);
        }
    }

    @Override
    public void delete(String ids) {
        final String regex = ",";
        List<Integer> list = new ArrayList<>();
        if (StringUtils.isEmpty(ids))
            return;
        /*for (String s : ids.split(regex)) {
            list.add(Integer.parseInt(s));
        }
        System.out.println(list);*/
//        supplierDao.deleteSupplierByIds(list);
        supplierDao.deleteSupplierByIds(ids.split(regex));
    }
}
